package com.itheima.log;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.LogConstant;
import com.itheima.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @version V1.0
 * @author: min
 * @date: 2019/8/8 上午8:28
 * @description:
 */
@Aspect
@Component
public class Logger {
    /**
     * 将日志信息保存到数据库
     **/
    @Reference
    private LogService logService;

    @Pointcut("execution(* com.itheima.controller.CheckGroupController.*(..))")
    public void checkGroupControllerLog() {
    }

    @Pointcut("execution(* com.itheima.controller.CheckitemController.*(..))")
    public void checkitemControllerLog() {
    }

    @Pointcut("execution(* com.itheima.controller.SetmealController.*(..))")
    public void setmealControllerLog() {
    }

    @Pointcut("execution(* com.itheima.controller.OrderSettingController.*(..))")
    public void orderSettingControllerLog() {
    }


    @AfterReturning(returning = "rvt", pointcut = "checkGroupControllerLog()")
    public void doAftercheckGroupControllerLog(JoinPoint joinPoint, Object rvt) {
        doAfter(joinPoint, LogConstant.OPERATE_NAME_CHECKGROUP, rvt);
    }

    @AfterReturning(returning = "rvt", pointcut = "checkitemControllerLog()")
    public void doAftercheckitemControllerLog(JoinPoint joinPoint, Object rvt) {
        doAfter(joinPoint, LogConstant.OPERATE_NAME_CHECKITEM, rvt);
    }

    @AfterReturning(returning = "rvt", pointcut = "setmealControllerLog()")
    public void doAftersetmealControllerLog(JoinPoint joinPoint, Object rvt) {
        doAfter(joinPoint, LogConstant.OPERATE_NAME_SETMEAL, rvt);
    }

    @AfterReturning(returning = "rvt", pointcut = "orderSettingControllerLog()")
    public void doAfterorderSettingControllerLog(JoinPoint joinPoint, Object rvt) {
        doAfter(joinPoint, LogConstant.OPERATE_NAME_ORDERSETTING, rvt);
    }

    /**
     * @param joinPoint
     * @param operateName:操作名称
     */
    public void doAfter(JoinPoint joinPoint, String operateName, Object rvt) {
        // 获取用户信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 操作者
        String operator = user.getUsername();
        // 操作时间
        LocalDateTime now = LocalDateTime.now();
        String time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now);
        // 操作类型
        // 操作类型，操作对象id记录数据库
        String operateTypeName = joinPoint.getSignature().getName();
        // 获取参数
        Object[] args = joinPoint.getArgs();
        logService.add(operator, time, operateName, operateTypeName, args, rvt);
    }
}